var express = require('express');
var router = express.Router();
const Query = require('../tools/db');
const { image_url } = require('../config/config');


//获取轮播图数据
router.get('/banners', async function (req, res, next) {
  let sql = `SELECT id,CONCAT('${image_url}','uploads/banner/',image_src) AS image_src,title FROM e_banners WHERE isshow = 1`
  let [err, result] = await Query(sql);
  //错误判断
  if (err) {
    next('服务器内部错误');
    return;
  }
  res.send({
    code: 200,
    msg: '获取成功',
    result,
  })
});

//获取首页课程数据
//同步：获取8条数据(type=1)
//精品：获取6条数据(type=2)
router.get('/courseinfo', async function (req, res, next) {
  let { type = 1 } = req.query;
  let num = type == 1 ? 8 : 6;
  let sql = `SELECT id,cid,title,price,type_name,area_name,subject_name,CONCAT('${image_url}','uploads/course/',image_src) AS image_src FROM e_course  WHERE type=${type} ORDER BY id DESC LIMIT ${num}`
  let [err, result] = await Query(sql);
  //错误判断
  if (err) {
    next('服务器内部错误');
    return;
  }
  res.send({
    code: 200,
    msg: '获取成功',
    result,
  })
});

module.exports = router;
